<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\RoleRepository;

class RolesController extends ManageBaseController
{

    private $roleRepo = NULL;

    public function __construct(RoleRepository $roleRepo)
    {
        parent::__construct();
        $this->roleRepo = $roleRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $menus = $this->roleRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($menus);
    }

    public function routes()
    {
        return retSuc([
            [
                "name" => "/system",
                "path" => "/system",
                "redirect" => "/system/user",
                "component" => 'Layout',
                "meta" => [
                    'hidden' => false,
                    'icon' => "system",
                    'title' => "系统管理",
                    'roles' => ['ADMIN6', 'GUEST', 'ADMIN']
                ],
                'children' => [
                    [
                        "path" => "user",
                        "name" => "User",
                        "component" => 'system/user/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "user",
                            "keepAlive" => true,
                            'title' => "用户管理",
                            'roles' => ['GUEST', 'ADMIN']
                        ],
                    ],
                    [
                        "path" => "role",
                        "name" => "Role",
                        "component" => 'system/role/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "role",
                            "keepAlive" => true,
                            'title' => "角色管理",
                            'roles' => ['GUEST', 'ADMIN6', 'ADMIN']
                        ],
                    ],
                    [
                        "path" => "menu",
                        "name" => "Menu",
                        "component" => 'system/menu/index',
                        "meta" => [
                            'hidden' => false,
                            'icon' => "menu",
                            "keepAlive" => true,
                            'title' => "菜单管理",
                            'roles' => ['ADMIN']
                        ],
                    ]
                ]
            ],
        ]);
    }

    public function menu_ids($role_id = null)
    {
        if (empty($role_id))
            return retFail("参数错误");

        $res = $this->roleRepo->getMenuIds($role_id);

        return retSuc($res);
    }

    /**
     * 角色分配菜单权限
     * @param null $role_id
     */
    public function auth_menu($role_id = null)
    {
        if (empty($role_id))
            return retFail("参数错误");

        $menuIds = request()->post("menuIds");
        if (!is_array($menuIds)) {
            return retFail("参数错误");
        }
        $this->roleRepo->assignMenu(intval($role_id), $menuIds);
        return retSuc(['menu_ids' => $menuIds]);
    }

    public function update($id = null)
    {
        $data = request()->all();
        $last_id = $this->roleRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail("更新失败");
        }

        $menu = $this->roleRepo->getOne($last_id);

        return retSuc($menu);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->roleRepo->getOne($id);
        return retSuc($res);
    }

    public function delete($id)
    {
        $ids = intval($id);
        if (empty($ids)) {
            return retFail("参数错误");
        }

        $res = $this->roleRepo->getOne($id);
        if ($res['code'] == 'SUPER') {
            return retFail($res['name'] . "不允许删除");
        }

        return $this->roleRepo->batchDelete($ids);
    }

    public function options()
    {
        $res = $this->roleRepo->getOptions();
        return retSuc($res);
    }
}
